<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>Virtual Earth Geocoverage</title>
        <script type="text/javascript" src="jquery-1.2.6.min.js"></script>
        <script type="text/javascript" src="wscaller.js"></script>
        <script type="text/javascript" src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2"></script>
        <script type="text/javascript">
var i=50;
$(document).ready(function()
{
    $.ajaxSetup({ global: false, type: "GET", url: "http://192.168.0.16:8080/geoserver/ows" });
    map = new VEMap('mapdiv');         
    processNextCity();
});

function processNextCity()     
{  
    if(i==0)
    {
        map.Dispose();
        location.reload() ; 
    }
    i=i-1;
    GetNextCity("VE", function(guid, y, x)
    {
        processZoomLevel(guid, y, x, 19,'a');
    }, LogMsg);
}

function processZoomLevel(guid, y, x, zoom, typemap)
{
    $("#mapdiv").css("width","50px");
    $("#mapdiv").css("height","50px");
    map.LoadMap(new VELatLong(y,x), zoom , typemap, true);
    if(typemap == 'r' && zoom == 17)
    {
        $("#mapdiv").css("width","800px");
        $("#mapdiv").css("height","800px");
    }
    window.setTimeout(processMap, 2000, guid, zoom, typemap, function()
    {
        if (typemap =='a')
        {
            typemap = 'r';
        }
        else
        {
            zoom = zoom-1;
            typemap = 'a';
        }
        if(zoom>8)
        {
            processZoomLevel(guid, y, x, zoom, typemap);
        }
        else
        {
            processNextCity();
        }
    }, 10);
}

function processMap(guid, zoom, typemap, callback, retry_count)
{
    tiles = '';
    if (typemap == 'a' || zoom == 17)
    {
        tilestable=GetTiles(typemap);
        if(typemap == 'r' && zoom == 17)
            nb_expected_tiles=16;
        else
            nb_expected_tiles=1;
        if (tilestable.length >= nb_expected_tiles ) 
            tiles=tilestable.join("|");
    }
    LogMsg("tiles=##### " + tiles + " ####### typemap=" + typemap + " zoom=" + zoom);
    if (tiles == '' && retry_count>0 && (typemap == 'a' || zoom == 17))
    {
        window.setTimeout(processMap, 1000, guid, zoom, typemap, callback, retry_count-1); //passing parameters to setTimeOut works only in FireFox
    }
    else if(retry_count>0)
    {
        be='';
        md='';
        res='';
        cp = GetCopyright();
        
        if (typemap == 'a')
        {
            md = GetMetadata();
            res = GetResolution();
            if(zoom == 19)
                be = GetBirdEyes();
        }     
        SendToServer(guid, zoom, typemap, tiles, cp, be, md, res, callback);
    }
    else
    {
        LogMsg("Tiles did not load after retries !");
        callback();
    }
}

function GetTiles()
{
    tab = new Array();
    j=0;
    $("#mapdiv > .MSVE_Map > .MSVE_ImageTile").each(function()
    {
        tab[j++]=$(this).attr("src");
    });
    
    return tab;
}

function GetCopyright()
{
	copyright = $("#mapdiv > .MSVE_Copyright").text();
	//LogMsg(copyright);
	return copyright;
}

function GetBirdEyes()
{
    boole = map.IsBirdseyeAvailable();
    if (boole)
        return 'true';
    else
        return '';
}

function GetMetadata()
{
//need a token
    return '';
}

function GetResolution()
{
    view = map.GetMapView();
    bottomright = view.BottomRightLatLong;
    topleft = view.TopLeftLatLong;
    
    res = ((topleft.Latitude - bottomright.Latitude)*60*1852)/800;
    return res;
}
       
function SendToServer(guid, zoom, typemap,tiles, cp, be, md, res, callback)
{
    SendTilesToServer(guid, zoom,typemap, tiles, function()
    {
        SendResultToServer(guid, zoom,typemap, 3, cp, 0, function()
        {
            SendResultToServer(guid, zoom,typemap, 2, be, 0, function()
            {
                SendResultToServer(guid, zoom,typemap, 5, md, 0, function()
                {
                    SendResultToServer(guid, zoom,typemap, 4, res, 2, function()
                    {
                        callback();
                    }, LogMsg );
                }, LogMsg );
            }, LogMsg );
        }, LogMsg );
    }, LogMsg );
}
        </script>
    </head>
    <body>  
        <div id='mapdiv' style="position:relative; width:50px; height:50px;"></div>
    </body>
</html>
